package com.kaifamiao.wendao.servlet;


import com.kaifamiao.wendao.service.TopicService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet( "/topic/remove/*" )
public class TopicRemoveServlet extends HttpServlet {

    @Override
    protected void doGet( HttpServletRequest request , HttpServletResponse response)
            throws ServletException, IOException {

        // 检查用户是否登录

        String uri = request.getRequestURI();
        System.out.println( uri );

        String idString = "" ;
        int last = uri.lastIndexOf( "/" );
        if( last != uri.length() - 1 ) {
            idString = uri.substring( last + 1 );
        }

        Integer topicId = null ;
        // 如果 idString 表示的是整数
        if( idString.matches( "^\\d+$" )){
            topicId = Integer.valueOf( idString );
        }

        TopicService ts = new TopicService();
        // 如果当前话题下有很多其它用户的评论，则不应该删除该话题
        ts.remove( topicId );

        response.sendRedirect( request.getContextPath() + "/topic/list/mine" );

    }
}
